package com.example.lab02.service;

import com.example.lab02.dao.UserDao;
import com.example.lab02.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    //    @Autowired
    private final UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public void create(User user) {
        userDao.create(user);
    }

    public List<User> queryAll() {
        return userDao.findAll(Sort.by(Sort.Direction.DESC, "id"));
    }

    public User queryById(int id) {
        return userDao.findById(id).orElse(null);
    }

    public void modify(User user) {
        userDao.save(user);
    }

    public Page<User> queryForPage(int page){
        Pageable pageable = PageRequest.of(page, 5);
       return userDao.findAll(pageable);
    }
}
